package org.destiny.swing.mapping;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import org.destiny.swing.exception.FieldMappingException;
import org.destiny.swing.mapping.base.BooleanInputMapper;
import org.destiny.swing.mapping.base.CheckBoxGroupMapper;
import org.destiny.swing.mapping.base.FileInputMapper;
import org.destiny.swing.mapping.base.RadioButtonGroupMapper;
import org.destiny.swing.mapping.base.TextComponentMapper;

public class FormMappingLoader {

	public static FormItemMapper getDefaultMappingComponent(Field field)
			throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException,
			SecurityException, NoSuchMethodException {
		Class<?> fieldType = field.getType();
		if (fieldType.equals(String.class)) {
			return new TextComponentMapper(field);
		} else if (fieldType.equals(Boolean.class) || fieldType.equals(boolean.class)) {
			return new BooleanInputMapper(field);
		} else if (fieldType.isEnum()) {
			return new RadioButtonGroupMapper(field, null);
		} else if (fieldType.isArray()) {
			return new CheckBoxGroupMapper(field);
		} else if (fieldType.equals(File.class)) {
			return new FileInputMapper(field);
		} else {
			throw new FieldMappingException("不能将字段[" + fieldType + "]映射为swing组件，没有匹配的组件类型");
		}
	}
}
